#coding=utf-8
import unittest
from selenium import webdriver
from appium import webdriver
from time import sleep
from nav_plugin_op import nav_plugin_factory
from selenium.common.exceptions import WebDriverException
import os
from Public import BasePage
from Tool.usefulTools import userfulToolsFactory
from Public.BasePage import element_Oprate
from Public.BasePage import BasicAction
from Public import BaseConstant
import DisposeCase
import settings

#实例化基础数据
navOpenFactory = nav_plugin_factory()
userfulTool = userfulToolsFactory()
element_oprate_instance = element_Oprate()
element_find_instances = BasicAction()
Casesupervise_Paramter_instances = BaseConstant.Casesupervise_Paramter
dealHelper_instances = DisposeCase.dispose_case_factory()
dealHelper_parameter = BaseConstant.DisposeCase
# nav_name = "工作"
# plugin_name = "案卷督办"
nav_name = ["导航栏工作",'xpath',"//android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.FrameLayout/android.widget.RadioButton"]
plugin_name = ["插件列表-案卷督办",'name','案卷督办']
send_opinion_keys = "自动化交办进行督办--请尽快处理"

class case_supervise_factory():

	def open_casesupervise_plug(self):
		try:
			element_oprate_instance.element_click(nav_name,settings.pic_file_path)
			BasePage.Base.driver.swipe(400,1000,400,50,1200)
			sleep(2)
			BasePage.Base.driver.swipe(400,1000,400,300,1200)
			sleep(4)
			# 点击"综合评价"的插件，打开插件
			element_oprate_instance.element_click(plugin_name,settings.pic_file_path)
			#等待综合评价页面内容进行加载，加载完成之后进行页面确认并界面
			if element_oprate_instance.element_display_commit(Casesupervise_Paramter_instances.supervise_name, settings.pic_file_path):

				# element_oprate_instance.element_click(Casesupervise_Paramter_instances.supervise_name, file_path)
				print u'打开督办案件箱子成功'
			else:
				print 'test failed'

		except WebDriverException,error:
			print error
			element_oprate_instance.element_save_pic(settings.pic_file_path, 'open_casesupervise_plug_fail')
			raise

	def case_supervise(self,file_path):
		try:
			#点击督办按钮
			element_oprate_instance.element_click(Casesupervise_Paramter_instances.supervise_btn_id, file_path)
			#选择答复天数
			element_oprate_instance.element_click(Casesupervise_Paramter_instances.replyDays_id, file_path)
			element_oprate_instance.element_click(Casesupervise_Paramter_instances.replyDays_keys, file_path)
			#点击发送短信
			element_oprate_instance.element_click(Casesupervise_Paramter_instances.sendmsg_sbn_id, file_path)
			#输入意见
			# element_oprate_instance.element_click(Casesupervise_Paramter_instances.send_opinion_name, file_path)
			element_oprate_instance.element_send_keys(Casesupervise_Paramter_instances.send_opinion_name,send_opinion_keys.decode("utf-8"), file_path)
			#督办语音
			element_oprate_instance.element_click(Casesupervise_Paramter_instances.opinion_btnRecord_id, file_path)
			sleep(3)
			element_oprate_instance.element_click(Casesupervise_Paramter_instances.opinion_btnStop_id, file_path)
			#截图
			element_oprate_instance.element_save_pic(file_path, 'case_supervise_successful')
			#点击发送
			element_oprate_instance.element_click(Casesupervise_Paramter_instances.opinion_btnSend_id, file_path)
			element_oprate_instance.element_click(Casesupervise_Paramter_instances.comfirm_id, file_path)
			element_oprate_instance.element_click(Casesupervise_Paramter_instances.exit_id, file_path)

		except WebDriverException,error:
			print error
			element_oprate_instance.element_save_pic(file_path, 'case_supervise_fail')
			raise

	#点击第一条案件
	def first_rec_click(self):
		sleep(2)
		#获取当前第一条案件的任务号
		task_num = element_find_instances.find_element(Casesupervise_Paramter_instances.supervise_task_num_location,settings.pic_file_path).get_attribute('text')
		print u'当前操作的案件号为： '+str(task_num)
		# 点击打开搜索结果--第一条案件
		element_oprate_instance.element_click(Casesupervise_Paramter_instances.supervise_task_num_location,settings.pic_file_path)
		#案件详情页面打开之后截图
		element_oprate_instance.element_save_pic(settings.pic_file_path,'dealhelp_first_rec_click_detail'+task_num)

supervise_factory_instances = case_supervise_factory()
class supervise_manager():
	#急要件督办
	def urgent_rec_supervise(self):
		#打开第一条案件
		try:
			element_oprate_instance.element_save_pic(settings.pic_file_path,'open_casesupervise_urgentRec_successful')
			supervise_factory_instances.first_rec_click()
			supervise_factory_instances.case_supervise(settings.pic_file_path)
		except Exception,error:
			print u'案件督办-急要件箱子，无案件可操作，直接抛出异常--failed'
			#调用返回功能列表
			dealHelper_instances.back_to_function_list()
			raise

	#超时件督办
	def overtime_rec_supervise(self):
		#打开督办箱子
		supervise_factory_instances.open_casesupervise_plug()
		element_oprate_instance.element_click(Casesupervise_Paramter_instances.overtime_rec_task,settings.pic_file_path)
		# 打开第一条案件
		try:
			element_oprate_instance.element_save_pic(settings.pic_file_path,'open_casesupervise_overtimeRec_successful')
			supervise_factory_instances.first_rec_click()
			supervise_factory_instances.case_supervise(settings.pic_file_path)
		except Exception, error:
			# 调用返回功能列表
			dealHelper_instances.back_to_function_list()
			print u'案件督办-超时件箱子，无案件可操作，直接抛出异常--failed'
			raise

	#督办件督办
	def suplist_rec_supervise(self):
		#打开督办箱子
		supervise_factory_instances.open_casesupervise_plug()
		element_oprate_instance.element_click(Casesupervise_Paramter_instances.suplist_rec_task,settings.pic_file_path)
		# 打开第一条案件
		try:
			element_oprate_instance.element_save_pic(settings.pic_file_path,'open_casesupervise_suplistRec_successful')
			supervise_factory_instances.first_rec_click()
			supervise_factory_instances.case_supervise(settings.pic_file_path)
		except Exception, error:

			# 调用返回功能列表
			dealHelper_instances.back_to_function_list()
			print u'案件督办-督办件箱子，无案件可操作，直接抛出异常--failed'
			raise


if __name__ == '__main__':
	unittest.main()

